package SubjectTree.One;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

import Utility.TreeNode;

public class PreorderTraversal {

/**
 * 难度：中等
 * 
 * 144. 二叉树的前序遍历
 * 	给你二叉树的根节点 root ，返回它节点值的 前序 遍历。
 * 
 * 示例 1：
 * 	输入：root = [1,null,2,3]
 * 	输出：[1,2,3]
 * 
 * 示例 2：
 * 	输入：root = []
 * 	输出：[]
 * 
 * 示例 3：
 * 	输入：root = [1]
 * 	输出：[1]
 * 
 * 示例 4：
 * 	输入：root = [1,2]
 * 	输出：[1,2]
 * 
 * 示例 5：
 * 	输入：root = [1,null,2]
 * 	输出：[1,2] 
 * 
 * 提示：
 * 	树中节点数目在范围 [0, 100] 内
 * 	-100 <= Node.val <= 100
 * 
 * 进阶：递归算法很简单，你可以通过迭代算法完成吗？
 * 
 * */
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		PreorderTraversal pt = new PreorderTraversal();
		TreeNode root = new TreeNode(1);
		root.left = null;
		root.right = new TreeNode(2);
		root.right.left = new TreeNode(3);
//		TreeNode root = TreeNode.MkTree("[1,null,2,3]");
		System.out.println(pt.preorderTraversal(root));
	}
	
	public List<Integer> preorderTraversal(TreeNode root) {
		List<Integer> list = new ArrayList<>();
		if(root==null) return list;
		Deque<TreeNode> deque = new LinkedList<>();
		TreeNode node = root;
		while(!deque.isEmpty()||node!=null) {
			while(node!=null) {
				list.add(node.val);
				deque.offer(node);
				node = node.left;
			}
			node = deque.poll();
			node = node.right;
		}
		return list;
	}
	//方法一：递归
	public List<Integer> preorderTraversal1(TreeNode root) {
		List<Integer> list = new ArrayList<>();
		traversal(root,list);
		return list;
    }
	public void traversal(TreeNode root,List<Integer> list) {
		if(root==null) return;
		list.add(root.val);
		traversal(root.left,list);
		traversal(root.right,list);
	}
	//方法二：迭代
	public List<Integer> preorderTraversal2(TreeNode root) {
        List<Integer> res = new ArrayList<Integer>();
        if (root == null) {
            return res;
        }
        Deque<TreeNode> stack = new LinkedList<TreeNode>();
        TreeNode node = root;
        while (!stack.isEmpty() || node != null) {
        	//一直向左取数，直到node = null跳出循环
            while (node != null) {
                res.add(node.val);
                stack.push(node);
                node = node.left;
            }
            //跳出循环后向右取数
            node = stack.pop();
            node = node.right;
        }
        return res;
    }
	//方法三：Morris 遍历
	public List<Integer> preorderTraversal3(TreeNode root) {
        List<Integer> res = new ArrayList<Integer>();
        if (root == null) {
            return res;
        }

        TreeNode p1 = root, p2 = null;

        while (p1 != null) {
            p2 = p1.left;
            if (p2 != null) {
                while (p2.right != null && p2.right != p1) {
                    p2 = p2.right;
                }
                if (p2.right == null) {
                    res.add(p1.val);
                    p2.right = p1;
                    p1 = p1.left;
                    continue;
                } else {
                    p2.right = null;
                }
            } else {
                res.add(p1.val);
            }
            p1 = p1.right;
        }
        return res;
    }
}
